﻿using System;
using Coleotrack.Data.Contracts;

namespace Coleotrack.Data
{
    public abstract class Entity : IEquatable<Entity>
    {
        private readonly int id;

        public virtual int Id
        {
            get { return id; }
        }

        protected Entity(int id)
        {
            this.id = id;
        }

        protected Entity()
        {
            
        }

        public virtual bool Equals(Entity other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return Id.Equals(default(int)) ? base.Equals(other) : other.Id.Equals(Id);
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as Entity);
        }

        public override int GetHashCode()
        {
            return Id.Equals(default(int)) ? base.GetHashCode() : Id.GetHashCode();
        }

        public static bool operator ==(Entity left, Entity right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(Entity left, Entity right)
        {
            return !Equals(left, right);
        }
    }
}